//-----------------------------------------------------------------------
// <copyright file="a_eCard_LogDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_eCard_LogDAOBase
    {
        #region Common methods
        public virtual a_eCard_Log Createa_eCard_LogFromReader(IDataReader reader)
        {
            a_eCard_Log item = new a_eCard_Log();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("LogID"))) item.LogID = (int)reader["LogID"];
                if (!reader.IsDBNull(reader.GetOrdinal("MsgID"))) item.MsgID = (int)reader["MsgID"];
                if (!reader.IsDBNull(reader.GetOrdinal("DateSend"))) item.DateSend = (DateTime)reader["DateSend"];
                if (!reader.IsDBNull(reader.GetOrdinal("DateRead"))) item.DateRead = (DateTime)reader["DateRead"];
                if (!reader.IsDBNull(reader.GetOrdinal("Status"))) item.Status = (byte)reader["Status"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_eCard_Log methods
            
        public virtual void Createa_eCard_Log(a_eCard_Log a_eCard_Log)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_eCard_Logs_ins");
                
                database.AddInParameter(dbCommand, "@MsgID", DbType.Int32, a_eCard_Log.MsgID);
                database.AddInParameter(dbCommand, "@DateSend", DbType.DateTime, a_eCard_Log.DateSend);
                database.AddInParameter(dbCommand, "@DateRead", DbType.DateTime, a_eCard_Log.DateRead);
                database.AddInParameter(dbCommand, "@Status", DbType.Byte, a_eCard_Log.Status);
                database.AddOutParameter(dbCommand, "@LogID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_eCard_Log.LogID = (int)database.GetParameterValue(dbCommand, "@LogID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_eCard_Log methods
        
        public virtual void Updatea_eCard_Log(a_eCard_Log a_eCard_Log)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_eCard_Logs_upd");            
                
                database.AddInParameter(dbCommand, "@LogID", DbType.Int32, a_eCard_Log.LogID);
                database.AddInParameter(dbCommand, "@MsgID", DbType.Int32, a_eCard_Log.MsgID);
                database.AddInParameter(dbCommand, "@DateSend", DbType.DateTime, a_eCard_Log.DateSend);
                database.AddInParameter(dbCommand, "@DateRead", DbType.DateTime, a_eCard_Log.DateRead);
                database.AddInParameter(dbCommand, "@Status", DbType.Byte, a_eCard_Log.Status);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_eCard_Log methods
        public virtual void Deletea_eCard_Log(int logID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_eCard_Logs_del");
                
                database.AddInParameter(dbCommand, "@LogID", DbType.Int32, logID);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_eCard_Log methods
        
        public virtual a_eCard_Log Geta_eCard_Log(int logID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_eCard_Logs_sel");
                
                database.AddInParameter(dbCommand, "@LogID", DbType.Int32, logID);
                
                a_eCard_Log a_eCard_Log =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_eCard_Log = Createa_eCard_LogFromReader(reader);
                        reader.Close();
                    }
                }
                return a_eCard_Log;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_eCard_LogList methods
        public virtual a_eCard_LogCollection Geta_eCard_LogList(a_eCard_LogColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_eCard_Logs_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_eCard_LogCollection a_eCard_LogCollection = new a_eCard_LogCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_eCard_Log a_eCard_Log = Createa_eCard_LogFromReader(reader);
                        a_eCard_LogCollection.Add(a_eCard_Log);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_eCard_LogCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_eCard_LogCollection Geta_eCard_LogList(a_eCard_LogColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_eCard_LogList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}